﻿using System.Collections.Specialized;
using DBFile.Core.Interfaces;
using DBFile.Providers.Impl;

namespace DBFile.Providers
{
    public class FtpProvider : DBFileProviderBase
    {
        
        protected override string ProviderDescription
        {
            get { return ProviderDescriptionMessage; }
        }
    
        protected override void  OnAfterInitialize(NameValueCollection configValue)
        {
            Username = GetConfigSettingOrThrow(configValue, UsernameKey, UsernameMissingMessage);
            Password = GetConfigSettingOrThrow(configValue, PasswordKey, PasswordMissingMessage);
            RepositoryPath = GetConfigSettingOrThrow(configValue, RepositoryPathKey, RepositoryPathMissingMessage);
        }
        
        protected override IDBFileProvider Provider
        {
            get
            {
                lock (padLock)
                {
                    if (_provider == null)
                    {
                        _provider = new FtpClient(Username, Password, RepositoryPath);
                    }
                }
                return _provider;
            }
        }

        internal string Username { get; private set; }
        internal string Password { get; private set; }
        internal string RepositoryPath { get; private set; }

        private const string ProviderDescriptionMessage = "Windows Azure Blob DBFile Provider";
        private const string UsernameKey = "username";
        private const string UsernameMissingMessage = "Username is not specified";
        private const string PasswordKey = "password";
        private const string PasswordMissingMessage = "Password is not specified";
        private const string RepositoryPathKey = "repositoryPath";
        private const string RepositoryPathMissingMessage = "Repository path is not specified";
    }
}
